import random

dictEnvironmentResult = {}

# **********************************************************************************

# set Environment for Lesson 1
def setEnvironment10():
    dictEnvironmentResult['e1'] = 'r1'
    dictEnvironmentResult['e2'] = 'r2'
    dictEnvironmentResult['e3'] = 'r3'
    dictEnvironmentResult['e4'] = 'r4'
    dictEnvironmentResult['e5'] = 'r5'
    dictEnvironmentResult['e6'] = 'r6'

# get the result for Lesson 1 after an Experiment
def getResultFromEnv10(experiment):
    result = ''
    if experiment in dictEnvironmentResult:
        result = dictEnvironmentResult[experiment]
    return result

# **********************************************************************************

# set Environment for Lesson 2
def setEnvironment20():
    dictEnvironmentResult['e1'] = ['r1', 'r2']
    dictEnvironmentResult['e2'] = ['r1', 'r2']
    dictEnvironmentResult['e3'] = ['r1', 'r2']
    dictEnvironmentResult['e4'] = ['r1', 'r2']
    dictEnvironmentResult['e5'] = ['r1', 'r2']
    dictEnvironmentResult['e6'] = ['r1', 'r2']

# get the result sequentially for Lesson 2 after an Experiment
def getResultSeqFromEnv20(experiment, lastResult):
    result = ''
    if experiment in dictEnvironmentResult:
        resultList = dictEnvironmentResult[experiment]
        i = 0
        if lastResult == '':
            result = resultList[0]
        else:
            while i < len(resultList):
                if resultList[i] == lastResult:
                    i += 1
                    break
                i += 1

            if i < len(resultList):
                result = resultList[i]
            else:
                result = resultList[0]
    return result

# get the result randomly for Lesson 2 after an Experiment
def getResultRandFromEnv20(experiment, lastResult):
    result = ''
    if experiment in dictEnvironmentResult:
        resultList = dictEnvironmentResult[experiment]
        result = random.choice(resultList)
        # Still avoid repeating last result for the same experiment
          # Comment out to have pure randomness i.e. last result may repeat
        #while result == lastResult:
        #    result = random.choice(resultList)
    return result

def getResultFromEnv20(experiment, lastResult, randomFlag = False):
    if randomFlag :
        result = getResultRandFromEnv20(experiment,lastResult)
    else:
        result = getResultSeqFromEnv20(experiment,lastResult)

    return result

# **********************************************************************************

# set Environment for Lesson 3
def setEnvironment30():
    dictEnvironmentResult['e1'] = ['r1', 'r2']
    dictEnvironmentResult['e2'] = ['r1', 'r2']
    dictEnvironmentResult['e3'] = ['r1', 'r2']
    dictEnvironmentResult['e4'] = ['r1', 'r2']
    dictEnvironmentResult['e5'] = ['r1', 'r2']
    dictEnvironmentResult['e6'] = ['r1', 'r2']

# get the result sequentially for Lesson 3 after an Experiment
def getResultSeqFromEnv30(experiment, lastExperiment):
    result = ''
    if experiment in dictEnvironmentResult:
        resultList = dictEnvironmentResult[experiment]
        if experiment == lastExperiment:
            result = resultList[0]
        else:
            result = resultList[1]
    return result

# get the result randomly for Lesson 3 after an Experiment
def getResultRandFromEnv30(experiment, lastResult):
    result = getResultRandFromEnv20(experiment, lastResult)
    return result


def getResultFromEnv30(experiment, lastExperiment, lastResult, randomFlag = False):
    if randomFlag :
        result = getResultRandFromEnv30(experiment,lastResult)
        print('Random Result: ' + result)
    else:
        result = getResultSeqFromEnv30(experiment,lastExperiment)
        print('Sequential Result: ' + result)
    return result

# **********************************************************************************
